
import {Category} from "../utils/api";

export default {//创建并导出vuex
    namespaced:true,
    state:{//存放保存在仓库中的数据
        isInit:false,
        list:[],//一级分类
        lump:[],//二级分类
    },
    mutations:{//更改state中的数据，只有mutations有资格
        _init(state,payload){
            state.list = payload.data;
            state.isInit = true;
        },
        _lump(state,payload){
            state.lump = payload;
            state.isInit = true;
        },
        _click(state,i){//点击切换左侧导航
            state.list.find((item)=>{
                item.activate = item.activate === 1 ? 0:0;
            });
            state.list[i].activate = 1;
        },
    },
    actions:{//执行一些异步耗时操作，比如请求数据
        init({state,commit,rootState:{$http}}){//rootState总仓库的$http解构
            if(!state.isInit){
                $http.post(Category.ALL)//左侧一级分类数据
                    .then(result => commit("_init",result));
            }
        },
        lump({state,commit,rootState:{$http}}){//右侧二级分类
            if(!state.isInit){
                $http.post(Category.LUMP)
                    .then(result => commit("_lump",result.data))
            }
        }
    },
};